# -*- coding: utf-8 -*-
# __author__ = 'yeslf'
# env: python 2.7 
# fuction: 检查当前出口IP地址是否变化，
# 如有变化就发送钉钉提示消息

import  urllib2
import  hashlib
import os


def sendmsg(msg):

    forqust=urllib2.Request('https://oapi.dingtalk.com/robot/send?access_token=xxxx')
    forqust.add_header("Content-Type","application/json")
    forqust.add_data('{"msgtype": "text","text": {"content":"o %s"}}' %msg )

    urllib2.urlopen(forqust)
  

def getip():
    forgetip=urllib2.Request('http://xxxx')
    forgetip.add_header("User-Agent","curl/7.29.0")
    tetresponse=urllib2.urlopen(forgetip).read()

    md5tool=hashlib.md5()
    md5tool.update(tetresponse)
    md5code=md5tool.hexdigest()

    # 检查空白文件是否存在，就创建一个
    if not os.path.exists('./md5file'):
        with open("./md5file","w") as f:
            f.write("")

    #前后内容比较
    with open("./md5file","r") as f:

        print(tetresponse)
        print(type(tetresponse))
        getfileread=f.read()

        if getfileread!=md5code  :
             with open("./md5file","w") as f:
                f.write(md5code)

             sendmsg(tetresponse)

getip()

